<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2023 https://www.meoyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 喵云科技 【https://www.meoyun.com】
// +----------------------------------------------------------------------

namespace app\common\controller;

use Webman\Http\Request;

class BaseMonoAdmin
{
    protected $dataModel;
    protected array $with = [];
    protected array $allowSearch = [];

    /**
     * 获取数据列表-分页数据
     * @param Request $request
     * @return mixed
     */
    public function getListByPage(Request $request)
    {
        $params = $request->get();
        $pageSize = $params['pageSize'] ?? 10;
        $queryForm = $params['queryForm'] ?? [];
        
        $query = $this->dataModel::query();
        
        // 处理搜索条件
        $searchWhere = array_intersect(array_keys($queryForm), $this->allowSearch);
        foreach ($searchWhere as $field) {
            if (!empty($queryForm[$field])) {
                $query->where($field, 'like', '%' . $queryForm[$field] . '%');
            }
        }
        
        // 添加关联查询
        if (!empty($this->with)) {
            $query->with($this->with);
        }
        
        $paginate = $query->orderBy('id', 'desc')->paginate($pageSize);
        
        return dataByPage($paginate);
    }

    /**
     * 获取数据列表
     * @param Request $request
     * @return mixed
     */
    public function getList(Request $request)
    {
        $query = $this->dataModel::query();
        
        // 添加关联查询
        if (!empty($this->with)) {
            $query->with($this->with);
        }
        
        $list = $query->orderBy('id', 'desc')->get();
        
        return data($list);
    }

    /**
     * 删除数据
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request)
    {
        $params = $request->post();
        $ids = $params['ids'] ?? [];
        
        if (empty($ids)) {
            return fail('请选择要删除的数据');
        }
        
        $res = $this->dataModel::destroy($ids);
        
        if ($res) {
            return success('删除成功');
        }
        
        return fail('删除失败');
    }

    /**
     * 获取单条数据信息
     * @param Request $request
     * @return mixed
     */
    public function info(Request $request)
    {
        $params = $request->get();
        $id = $params['id'] ?? '';
        
        if (empty($id)) {
            return fail('ID参数不能为空');
        }
        
        $query = $this->dataModel::query();
        
        // 添加关联查询
        if (!empty($this->with)) {
            $query->with($this->with);
        }
        
        $info = $query->find($id);
        
        if (!$info) {
            return fail('数据不存在');
        }
        
        return data($info);
    }

    /**
     * 保存数据（新增或更新）
     * @param Request $request
     * @return mixed
     */
    public function save(Request $request)
    {
        $params = $request->post();
        $id = $params['id'] ?? '';
        
        try {
            if ($id) {
                // 更新
                $dataModel = $this->dataModel::find($id);
                if (!$dataModel) {
                    return fail('数据不存在');
                }
                unset($params['id']); // 移除ID字段，避免更新主键
                $dataModel->update($params);
                return success('更新成功');
            } else {
                // 新增
                $this->dataModel::create($params);
                return success('创建成功');
            }
        } catch (\Exception $e) {
            return fail('操作失败：' . $e->getMessage());
        }
    }
}